package com.manor.paradise.dialog

import android.content.Context
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.text.method.PasswordTransformationMethod
import android.view.View
import com.bumptech.glide.Glide
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.dialog_prop.view.*
import java.math.BigDecimal

/**
 * 输入交易密码
 * @property sdCoupons BigDecimal
 * @property propsId String
 * @constructor
 */

class PropsPopup(context: Context,var sdCoupons:BigDecimal ,var propsId :String) : FullScreenPopupView(context), View.OnClickListener {
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_prop
    }

    override fun onCreate() {
        super.onCreate()
        im_dismiss.setOnClickListener(this)
        ed_password.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_password.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_password.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框
        tv_name.setText("你将扣除"+sdCoupons.toString()+"嗨券")
        tv_confirm.setOnClickListener({
            if(!TextUtils.isEmpty(ed_password.text.toString())&&ed_password.text.toString().length==6){
                ElvesHttpUtil.getPropsList(ed_password.text.toString(),propsId,sdCoupons, object : JHttpCallback() {
                    override fun onSuccess(code: Int, msg: String, info: String) {

                        if (code != 200) {
                            ToastUtil.show(msg)
                            return
                        }

                        dismiss()

                    }

                    override fun onError() {
                        super.onError()

                    }
                })
            }else{
                if(ed_password.text.toString().length<6){
                    ToastUtil.show("请输入6位交易密码")
                }
                if(TextUtils.isEmpty(ed_password.text.toString())){
                    ToastUtil.show("请输入交易密码")
                }
            }

        })
        ElvesHttpUtil.getParadiseInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var paradiseInfoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                tv_ks.setText("我有淘米劵：" + paradiseInfoBean.sdCoupons.toString())

            }

            override fun onError() {
                super.onError()

            }
        })
    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    dismiss()
                }
            }
    }
    override fun onDestroy() {
        Glide.get(context).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_BY_UID)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_PROPS_BUY)
        super.onDestroy()
    }
}